package com.zhang.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhang.system.entity.Role;
import com.zhang.system.entity.UserRole;
import com.zhang.system.mapper.UserRoleMapper;
import com.zhang.system.service.IRoleService;
import com.zhang.system.service.IUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 关联表：用户与角色 服务实现类
 * </p>
 *
 * @author 创世神
 * @since 2023-07-06
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements IUserRoleService {
    @Autowired
    private IRoleService roleService;

    @Override
    public List<Role> getUserRoleInfo(Long userId) {
        //查询关联表
        QueryWrapper<UserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        List<UserRole> userRole = list(wrapper);
        //查询角色表
        List<Long> roleIds = userRole.stream().map(UserRole::getRoleId).collect(Collectors.toList());
        if (roleIds.size() > 0) {
            return roleService.listByIds(roleIds);
        } else return new ArrayList<>();
    }

    @Override
    public List<Long> getUserRoleIds(Long userId) {
        LambdaQueryWrapper<UserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserRole::getUserId, userId);
        List<UserRole> list = list(wrapper);
        List<Long> collect = list.stream().map(UserRole::getRoleId).collect(Collectors.toList());
        return collect;
    }
}
